package com.szzg10.webservice.chapter3;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

import com.szzg10.webservice.chapter3.wsimport.echo.Echo;
import com.szzg10.webservice.chapter3.wsimport.echo.EchoService;

public class EchoClient {

	private static final String URL = "http://localhost:9876";
	private static final String SERVICE = "echo";
	
	public static void main(String[] args) {
		
		EchoService service = new EchoService();
		Echo port = service.getEchoPort();
		
		Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext();
		requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, URL + "/" + SERVICE);
		requestContext.put(BindingProvider.SOAPACTION_URI_PROPERTY, SERVICE);
		
		Map<String, List<String>> myHeader = new HashMap<String, List<String>>();
		myHeader.put("Accept-Encoding", Collections.singletonList("gzip"));
		myHeader.put("Greeting", Collections.singletonList("Hello, world!"));
		
		requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, myHeader);
		MapDump.dump_map(requestContext, "");
		
		System.out.println("\n\nRequest above, response below\n\n");
		
		String response = port.echo("Have a nice day :)");
		System.out.println(response);
		
		Map<String, Object> responseContext = ((BindingProvider)port).getResponseContext();
		MapDump.dump_map(responseContext, "");
		
		Object response_code = responseContext.get(MessageContext.HTTP_RESPONSE_CODE);
		System.out.println(response_code);
	}
}
